package com.sugon.modules.excel.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.entity.ModuleTypeEntity;
import com.sugon.entity.MoldTypePositionEntity;
import com.sugon.modules.excel.IExcelHandler;
import com.sugon.modules.excel.dto.MoldTypePositionImportParamDTO;
import com.sugon.service.ModuleTypePositionService;
import com.sugon.service.ModuleTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 模具类型关联位置Excel导入Handler
 *
 * @author YuXD
 * @since 2023-02-25
 */
@Service("moldTypePositionExcelHandler")
public class MoldTypePositionExcelHandler extends AbstractExcelHandler implements IExcelHandler {

    @Resource
    private ModuleTypeService moldTypeService;
    @Resource
    private ModuleTypePositionService moldTypePositionService;

    /**
     * @param excelInputStream excel输入流
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int importExcelFromStream(InputStream excelInputStream) throws Exception {
        // 获取导入配置
        ImportParams importParams = getImportParams();
        // 从上传文件中读取起重设备台账列表
        List<MoldTypePositionImportParamDTO> moldTypePosList = ExcelImportUtil.importExcel(excelInputStream, MoldTypePositionImportParamDTO.class, importParams);
        if (CollectionUtil.isEmpty(moldTypePosList)) {
            return 0;
        }

        // 获取模具类型匹配
        List<ModuleTypeEntity> moldTypeList = moldTypeService.queryList(null);
        Map<String, String> moldTypeMap = moldTypeList.stream().collect(Collectors.toMap(ModuleTypeEntity::getTypeCode, ModuleTypeEntity::getId));
        int importRows = 0;
        MoldTypePositionEntity moldTypePos = null;
        for (MoldTypePositionImportParamDTO moldTypePosParam : moldTypePosList) {

            String moldTypeCode = moldTypePosParam.getMoldTypeCode();
            if (StrUtil.isEmpty(moldTypeCode)) {
                continue;
            }
            String moldTypeId = moldTypeMap.get(moldTypeCode);
            if (CollectionUtil.isEmpty(moldTypeMap)) {
                continue;
            }
            // 每个模具类型位置关联下：模具类型、备件名称、位置号
            List<MoldTypePositionEntity> existedPosList = moldTypePositionService.queryList(moldTypeId, moldTypePosParam.getPositionName(), moldTypePosParam.getPositionNo());
            if (CollectionUtil.isNotEmpty(existedPosList)) {
                moldTypePos = existedPosList.get(0);
            } else {
                moldTypePos = new MoldTypePositionEntity();
                moldTypePos.setMoldTypeId(moldTypeId);
            }
            BeanUtil.copyProperties(moldTypePosParam, moldTypePos);

            if (StrUtil.isEmpty(moldTypePos.getId())) {
                moldTypePositionService.save(moldTypePos);
            } else {
                moldTypePositionService.update(moldTypePos);
            }
            importRows += 1;
        }
        return importRows;
    }

    /**
     * 设置导入参数
     *
     * @return
     */
    @Override
    public ImportParams getImportParams() {
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        params.setKeyIndex(0);
        return params;
    }

}
